﻿using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;

using System.Linq;

namespace JULONG.WEB.Library
{   
    public static partial class staticClass
    {
        //TrySplitHelper
        public static string[] TrySplit(this string str, char separator)
        {
            if (str == null || str == "") return new string[] { "" };
            return str.Split(separator);
        }

        public static string[] TrySplit(this string str, string separator)
        {
            if (str == null || str == "") return new string[] { "" };

            return Regex.Split(str, separator);
        }

        //字符串处理

        //字符串截短
        public static string deflate(this string str, int size)
        {
            if (str == null) return "";
            if (str.Length < size) return str;
            return str.Remove(size - 1) + "...";
        }

        /// <summary>
        /// 对字符串进行encode编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string encode(this string str)
        {
            if (str == null) return "";
            return HttpUtility.HtmlEncode(str);
        }

        /// <summary>
        /// 移出字符串中Html代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string reHtmlCode(this string str)
        {
            return System.Text.RegularExpressions.Regex.Replace(str, "<[^>]+>", "");
        }
        /// <summary>
        /// 尝试截断字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string reSubstring(this string str,int end)
        {
            if (!string.IsNullOrWhiteSpace(str) && str.Length>0) {
                if (str.Length - 1 >= end)
                {
                    
                    str.Substring(0, end);
                }
                else {
                    return str;
                }
            }
            return "";
        }
        /// <summary>
        /// 进行MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(this string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str.Trim(), "MD5");
        }

        /// <summary> 
        /// 取得HTML中所有图片的 URL。 
        /// </summary> 
        /// <param name="sHtmlText">HTML代码</param> 
        /// <returns>图片的URL列表</returns> 
        public static List<string> GetHtmlImageUrlList(this string htmlStr)
        {

            // 定义正则表达式用来匹配 img 标签 
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

            // 搜索匹配的字符串 
            MatchCollection matches = regImg.Matches(htmlStr);

            List<string> sUrlList = new List<string>();
            foreach (Match match in matches)
                sUrlList.Add(match.Groups["imgUrl"].Value);

            return sUrlList;

        }

    }
}
